<?php
/**
 * excel类,解析excel
 * @param string $filePath
 * @param int $sheet
 * @return array|void
 * @throws \PHPExcel_Exception
 * @throws \PHPExcel_Reader_Exception
 */
function format_excel2array($filePath='',$sheet=0){
    include('./Org/phpexcel/Classes/PHPExcel.php');
    if (empty($filePath) or !file_exists($filePath)) {
        die('file not exists');
    }
    $PHPReader = new \PHPExcel_Reader_Excel2007();        //建立reader对象
    if (!$PHPReader->canRead($filePath)) {
        $PHPReader = new \PHPExcel_Reader_Excel5();
        if (!$PHPReader->canRead($filePath)) {
            echo 'no Excel';
            return;
        }
    }
    $PHPExcel = $PHPReader->load($filePath);        //建立excel对象
    $currentSheet = $PHPExcel->getSheet($sheet);        //**读取excel文件中的指定工作表*/
    $allColumn = $currentSheet->getHighestColumn();        //**取得最大的列号*/
    $allRow = $currentSheet->getHighestRow();        //**取得一共有多少行*/
    $data = array();
    for ($rowIndex = 1; $rowIndex <= $allRow; $rowIndex++) {        //循环读取每个单元格的内容。注意行从1开始，列从A开始
        for ($colIndex = 'A'; $colIndex <= $allColumn; $colIndex++) {
            $addr = $colIndex . $rowIndex;
            $cell = $currentSheet->getCell($addr)->getValue();
            if ($cell instanceof PHPExcel_RichText) { //富文本转换字符串
                $cell = $cell->__toString();
            }
            $data[$rowIndex][$colIndex] = $cell;
        }
    }
    return $data;
}

/**
 * 连接redis服务器
 * @return Redis
 */
function linkredis(){
    $redisip = C('redis_host');
    $redispasswd = C('redis_passwd');
    $redisport = C('redis_port');
    $redis = new \Redis();
    if(!$redis->connect($redisip,$redisport)){
        exit('redis数据库连接失败');
    }
    if(!$redis->auth($redispasswd)){
        exit('redis数据库认证登陆失败');
    }
    $redis->select(0);//切换到第0个库 components项目用第0个数据库
    return $redis;
}

/**根据单个的产品系列ID取出该系列下的筛选条件
 * @param $cpxlno
 * @return array|mixed
 */
function getcpxlwzbmfilter($cpxlno){
    if($data = S($cpxlno.'_xl_wzbmfilter')){
        $data = unserialize($data);
        return $data;
    }else{
        $b_p_cpxlwzbm = D('b_p_cpxlwzbm');
        $b_c_wzbm = D('b_c_wzbm');
        //取出物资编码ID
        $wznoinfo = $b_p_cpxlwzbm->field('wzno')->where("cpxlno={$cpxlno}")->select();
        $wznos = array_column($wznoinfo, 'wzno');
        $filter = array();//筛选初始数据

        //取出该系列的属性筛选数据
        $b_c_zdysx = D('b_c_zdysx a');
        $info = $b_c_zdysx->field('b.sxno,b.sxmc,b.sxdw,GROUP_CONCAT(a.zdysxz) sxz')
            ->join('left join b_c_sxgz b on a.sxno=b.sxno')
            ->where(array(
                'wzno' => array('in',$wznos)
            ))->group('b.sxno')->select();

        foreach($info as $k=>&$v){
            $v['sxz'] = array_unique(explode(',',$v['sxz']));
            if(count($v['sxz']) < 2 ){    //判断去重之后的属性值如果少于2个,就不做筛选条件
                unset($info[$k]);
            }
        }
        $filter['sx'] = $info;

        //取出该系列的型号筛选数据
        $xh = $b_c_wzbm->alias('a')->field('xh')->where(array(
            'a.wzno' => array('in',$wznos)
        ))->group('xh')->select();
        $xh = array_column($xh,'xh');
        if(count($xh) > 1){
            $filter['xh'] = $xh;
        }
        $data = array(
            'filterifno' => $filter,
            'wznos' => $wznos
        );
        S($cpxlno.'_xl_wzbmfilter',serialize($data),3600*48);
        return $data;
    }
}

/**
 * 显示图片函数
 * @param $url 要显示的图片
 * @param string $width  图片的宽
 * @param string $height  图片的高
 * @param int $group  图片分组
 */
function showimage($url,$group,$width='',$height=''){
    $imageviewpath = C('IMAGE_CONFIG.VIEWPATH');
    if($width){
        $width = 'width='.$width;
    }elseif($height){
        $height = 'height='.$height;
    }
    echo "<img $width $height src='{$imageviewpath}$url' data-preview-src='' data-preview-group='$group'/>";
}

/**
 * 删除文件里的单张图片
 * @param $id  系列扩展ID
 * @param $dbname  表名
 * @param $imgname  图片字段
 */
function delOneimg($id,$dbname,$imgname){
    $path = C('IMAGE_CONFIG.ROOTPATH');
    $dbmodel = D($dbname);
    $info = $dbmodel->field('jgh_img,dhsl_img')->find($id);
    unlink($path.$info[$imgname]);
}

/**删除一个系列的所有图片
 * @param $cpxlno  系列编码
 * @param $obj  实例化后的对象
 * @param $imgname  图片字段名
 */
function delAllimg($cpxlno,$obj,$imgname){
    $path = C('IMAGE_CONFIG.ROOTPATH');
    $info = $obj->field($imgname)->where("cpxlno={$cpxlno}")->select();

    foreach($info as $k=>$v){
        unlink($path.$v[$imgname]);
    }

}

//CURL请求的函数http_request()
//通过https 中的get 或 post
function https_request($url, $data = null){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

/**
 * 获取accesstoken
 * @param $corpid 企业号ID
 * @param $secret  管理组的凭证密钥
 * @return bool|string
 */
function componentsgetaccesstoken(){
    $corpid = C('CORPID');
    $secret = C('SECRET');
    $redis = linkredis();
    if(!$token = $redis->get('components_accesstoken')){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$secret}";
        $info = https_request($url);
        $accesstoken = json_decode($info,true)['access_token'];
        $redis->set('components_accesstoken',$accesstoken);
        $redis->expire('components_accesstoken',7100);
        return $accesstoken;
    }else{
        return $token;
    }
}

/**
 *  获取jssdk的临时票据ticket
 * @param $access_token
 * @return bool|string
 */
function get_components_ticket($access_token){
    $redis = linkredis();
    if($jsapi_ticket = $redis->get('components_ticket')){
        return $jsapi_ticket;
    }else{
        $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token={$access_token}";
        $info = https_request($url);
        $ticket = json_decode($info,true)['ticket'];
        $redis->set('components_ticket',$ticket);
        $redis->expire('components_ticket',7100);
        return $ticket;
    }
}

